<?php
    if(!defined('LT2N_ADMIN')){
        die('bạn không có quyền truy cập');
    }
    $db = new LoaiTaiSan();
    $module['name'] = $_GET["mod"];
    if($_GET['mod'] == 'loaitaisan'){

        $act = $_GET['act'];
        if($act == '' || $act == 'list'){
            $act = 'list';
            $arr = $db->select(LT2N_PREFIX.'_loaitaisan','','ORDER BY `ma_loai_tai_san` ASC');
            require(LT2N_VIEW_PATH.$module["name"]."/".$act.".php");

        } elseif($act == 'add'){

            initalize_post_var('ma_loai_tai_san','ten_loai_tai_san');

            if(isset($_SESSION['permission']) && $_SESSION['permission'] != PERMISSION_USER ){
                if(isset($_POST['btnAdd'])){
                    $ma_loai_tai_san    = $_POST['ma_loai_tai_san'];
                    $ten_loai_tai_san    = $_POST['ten_loai_tai_san'];

                    if(!$db->ton_tai_loai_tai_san($ma_loai_tai_san)){
                        $list = array(
                                'ma_loai_tai_san'=> strtoupper($ma_loai_tai_san),
                                'ten_loai_tai_san'=>$ten_loai_tai_san
                        );


                        $db->insert(LT2N_PREFIX.'_loaitaisan',$list);
                        if($db->checkQuery() == 1){
                            //$success[] = '<div class="success" >Đã thêm thành công.</div>';
                            header("Location: index.php?mod=loaitaisan&act=list&success=".SUCCESS_ADD);
                        } else {
                            $errors[] = '<div class="error" >Thêm loại tài sản mới thất bại.</div>';
                        }

                    } else {
                        $errors[] = '<div class="error" >Mã loại tài sản này đã tồn tại.</div>';
                    }

                }

                require(LT2N_VIEW_PATH.$module["name"]."/".$act.".php");
            } else {
                header('location: index.php?mod=loaitaisan&act=list&err='.ERR_NOT_PERMISSION);
            }


        }elseif($act == 'edit') {

            if(isset($_SESSION['permission']) && $_SESSION['permission'] != PERMISSION_USER ){
                if(isset($_POST['btnUpdate'])){

                    $ten_loai_tai_san    = $_POST['ten_loai_tai_san'];
                    $list = array(
                            'ten_loai_tai_san'=>$ten_loai_tai_san
                    );


                    $key = "`loaitaisan_id` = ". $_GET['id'];

                    $db->update(LT2N_PREFIX.'_loaitaisan', $list, $key);
                    header("Location: index.php?mod=loaitaisan&act=list&success=".SUCCESS_EDIT);

                }

                $arr = $db->select(LT2N_PREFIX.'_loaitaisan',array('loaitaisan_id'=>$_GET['id']));
                require(LT2N_VIEW_PATH.$module["name"]."/".$act.".php");

            } else {
                header('location: index.php?mod=loaitaisan&act=list&err='.ERR_NOT_PERMISSION);
            }



        } elseif($act == 'del' ){

            if(isset($_SESSION['permission']) && $_SESSION['permission'] == PERMISSION_ADMIN ){
                $truyvan = "SELECT * FROM tscd_loaitaisan a, tscd_danhmuctaisan b WHERE a.loaitaisan_id = b.loaitaisan_id AND a.loaitaisan_id = '".$_GET['id']."'";
                $db->setSql($truyvan);
                $db->query();

                if($db->numrow($db->getDt()) == 0){
                    $db->delete(LT2N_PREFIX.'_loaitaisan',array('loaitaisan_id'=>$_GET['id']));
                    header('location: index.php?mod=loaitaisan&act=list&success='.SUCCESS_DELETE);
                } else {
                    header('location: index.php?mod=loaitaisan&act=list&err='.ERR_DELETE);
                }

            } else {
                header('location: index.php?mod=loaitaisan&act=list&err='.ERR_NOT_PERMISSION);
            }


        }


    }
?>